﻿using System;
using System.Web.UI;
using NUnit.Framework;
using OpenWaves.Web;

[assembly: PublicAssemblyResources("OpenWaves.Web.UnitTests.PublicResource.txt")]
[assembly: PublicAssemblyResources("OpenWaves.Web.UnitTests.MissingResource.txt")]

namespace OpenWaves.Web.UnitTests
{
    [TestFixture]
    public class AssemblyResourceVirtualPathProviderTests
    {
        [Test]
        public void Given_embeded_file_GetFileHash_includes_assembly_identity()
        {
            var provider = new AssemblyResourceVirtualPathProvider();

            var hash = provider.GetFileHash("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.PublicResource.txt", new[] { "~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.PublicResource.txt" });

            Assert.That(hash, Is.Not.Empty);
        }

        [Test]
        public void Given_file_is_marked_as_PublicResource_GetFile_returns_valid_file()
        {
            var provider = new AssemblyResourceVirtualPathProvider();

            var file = provider.GetFile("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.PublicResource.txt");

            Assert.IsNotNull(file);

            using (var s = file.Open())
            {
                Assert.IsTrue(s.CanRead);
            }
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Attempt_to_open_file_not_market_as_PublicResource_throws()
        {
            var provider = new AssemblyResourceVirtualPathProvider();

            var file = provider.GetFile("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.PrivateResource.txt");
            file.Open();
        }

        [Test]
        public void Attempt_to_open_non_existent_file_returns_null()
        {
            var provider = new AssemblyResourceVirtualPathProvider();

            Assert.IsNull(provider.GetFile("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.NoSuchFile.txt"));
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Attempt_to_open_missing_file_throws()
        {
            var provider = new AssemblyResourceVirtualPathProvider();

            var file = provider.GetFile("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.MissingResource.txt");
            file.Open();
        }

        [Test]
        public void CreateUrl_returns_correct_url()
        {
            var url = AssemblyResourceVirtualPathProvider.CreateUrl(typeof(TestPage));

            Assert.That(url.ToString(), Is.EqualTo("~/App_Resource/OpenWaves.Web.UnitTests/OpenWaves.Web.UnitTests.TestPage.aspx"));
        }

        [Test]
        public void WhenUrlToMasterPageIsCreatedThenEndsWithCorrectExtension()
        {
            var url = AssemblyResourceVirtualPathProvider.CreateUrl(typeof(TestMasterPage));

            Assert.That(url.ToString(), Is.StringEnding(".master"));
        }
    }

    public class TestPage : Page
    {
    }


    public class TestMasterPage : MasterPage
    {

    }
}